package cn.ym.mall.service.impl;

import cn.ym.mall.dao.AnnouncementDao;
import cn.ym.mall.dao.impl.AnnouncementDaoImpl;
import cn.ym.mall.entity.Announcement;
import cn.ym.mall.entity.Order;
import cn.ym.mall.service.AnnouncementService;
import cn.ym.mall.utils.DruidUtils;
import cn.ym.mall.vo.Page;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 赵金明
 * 2022/8/12
 */
public class AnnouncementServiceImpl implements AnnouncementService {
    private AnnouncementDao announcementDao = new AnnouncementDaoImpl();
    /**
     * 添加公告
     * @param announcement
     * @throws SQLException
     * @return返回成功的条数
     */
    @Override
    public boolean addAnnouncement(Announcement announcement) throws SQLException {
        return announcementDao.addAnnouncement(announcement) == 1;
    }

    /**
     * 通过公告id删除公告
     * @param aceid
     * @return
     * @throws SQLException
     */
    @Override
    public boolean deleteByAceid(Integer aceid) throws SQLException {
        return announcementDao.deleteByAceid(aceid) == 1;
    }

    /**通过公告id查询公告
     * @param aceid
     * @return
     * @throws SQLException
     */
    @Override
    public Announcement getAnnouncementByAceid(Integer aceid) throws SQLException {
        Announcement announcement=null;
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            announcement = announcementDao.getAnnouncementByAceid(aceid);
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return announcement;
    }

    /**
     * 分页查询公告
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Announcement> getPageAnnouncement(Integer pageNo, Integer pageSize) throws SQLException {
        Page<Announcement> page = new Page<Announcement>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setCount((int)announcementDao.getAnnouncementCount());
        page.setList(announcementDao.getPageAnnouncement(pageNo,pageSize));
        return page;
    }

}
